#!/bin/bash
#########################################
# run-problem2.sh
# 支持：
#   --combiner=true/false
#   --partitioner=alphabet/hash/length
#########################################

BASE_DIR="/home/hadoop/hw2_hadoop/java_code/s522025320243-邹丁-hadoop-assignment"
SRC_DIR="${BASE_DIR}/src/main/java"
TARGET_DIR="${BASE_DIR}/target"
CLASSES_DIR="${TARGET_DIR}/classes"
JAR_FILE="${BASE_DIR}/hadoop-assignment.jar"

STUDENT_ID="s522025320243"
MAIN_CLASS="com.bigdata.assignment.problem2.WordCountDriver"
INPUT_DIR="/public/data/wordcount"
OUTPUT_DIR_BASE="/users/${STUDENT_ID}/homework1/problem2"

LOCAL_SAVE_DIR="${BASE_DIR}/output/problem2"

# 默认参数
USE_COMBINER=true
PARTITIONER="alphabet"

# 参数解析
for arg in "$@"; do
    case $arg in
    --combiner=*)
        USE_COMBINER="${arg#*=}"
        ;;
    --partitioner=*)
        PARTITIONER="${arg#*=}"
        ;;
    esac
done

# 输出目录（按配置区分）
OUTPUT_DIR="${OUTPUT_DIR_BASE}/combiner-${USE_COMBINER}-partitioner-${PARTITIONER}"

echo "==============================="
echo "  🚀 Problem 2 (combiner=${USE_COMBINER}, partitioner=${PARTITIONER})"
echo "==============================="

echo "🧹 清理旧目录..."
rm -rf "${CLASSES_DIR}" "${JAR_FILE}"
mkdir -p "${CLASSES_DIR}" "${LOCAL_SAVE_DIR}"

echo "🔨 编译 Java..."
javac -cp "$(hadoop classpath)" -d "${CLASSES_DIR}" $(find "${SRC_DIR}" -name "*.java")
[ $? -ne 0 ] && { echo "❌ 编译失败"; exit 1; }

echo "📦 打包 JAR..."
jar -cvf "${JAR_FILE}" -C "${CLASSES_DIR}" .

# Hadoop 执行前删除旧 output
echo "🧹 删除旧 HDFS 输出目录：${OUTPUT_DIR}"
hdfs dfs -rm -r -f "${OUTPUT_DIR}" >/dev/null

# 提交 Hadoop 作业
echo "🚀 提交 Hadoop 作业..."
hadoop jar "${JAR_FILE}" ${MAIN_CLASS} \
    ${INPUT_DIR} ${OUTPUT_DIR} \
    ${PARTITIONER} \
    ${USE_COMBINER}
    

[ $? -ne 0 ] && { echo "❌ Hadoop 作业失败"; exit 1; }

# 下载结果
echo "💾 下载结果到本地目录: ${LOCAL_SAVE_DIR}"
mkdir -p "${LOCAL_SAVE_DIR}"
hdfs dfs -get -f "${OUTPUT_DIR}/words.txt" "${LOCAL_SAVE_DIR}/words-${PARTITIONER}-${USE_COMBINER}.txt"
hdfs dfs -get -f "${OUTPUT_DIR}/statistics.txt" "${LOCAL_SAVE_DIR}/statistics-${PARTITIONER}-${USE_COMBINER}.txt"

echo "🎉 完成！"
